package com.ruiyi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruiyi.common.Result;
import com.ruiyi.domain.User;
import com.ruiyi.dto.UserDto;
import com.ruiyi.dto.UserSearchDto;
import com.ruiyi.mapper.UserMapper;
import com.ruiyi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result findUsersByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> userList = userMapper.selectUsersByPage();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        Result result = new Result();
        result.setData(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    @Override
    public Result saveUser(UserDto userDto) {
        //判断是否有部门
        int i = userMapper.selectDeptIsExist(userDto);
        if(i == 0){
            return new Result(-1,"部门不存在，不能进行添加!");
        }
        //判断要添加的信息是否已经存在
        int j = userMapper.selectUsersIsExist(userDto);
        if(j == 1){
            return new Result(-1,"记录已经存在，不能重复添加!");
        }
        //添加新的信息
        int n = userMapper.insertUser(userDto);
        if(n == 0){
            return new Result(-1,"添加失败!");
        }
        return new Result();
    }

    /*
    * id + "," 表示拼接多个id
    * [1-9][0-9]*表示匹配一个正整数，其中[1-9]表示设置第一个数字不是0（id的值不能是0，也第一个数不能为0）
    * [0-9]*表示任意多个数字 （比如id=100,就是0被匹配了两次）
    * + 重复一次或者更多次
    * */
    @Override
    public Result removeManyUser(String id) {
        if(id == null || !((id + ",").matches("([1-9][0-9]*,)+"))){
            return Result.DATA_FORMAT_ERROR;
        }
        int i = userMapper.deleteManyUser(id);
        if(i == 0){
            return new Result(-1,"删除失败");
        }
        return new Result();
    }

    @Override
    public Result findUsersBySearch(UserSearchDto userSearchDto) {
        PageHelper.startPage(userSearchDto.getPageNum(),userSearchDto.getPageSize());
        List<User> userList = userMapper.selectUsersBySearch(userSearchDto);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        Result result = new Result();
        result.setData(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    @Override
    public Result removeOneUser(Long id) {
        if(id <= 0){
            return Result.DATA_FORMAT_ERROR;
        }
        int i = userMapper.deleteOneUser(id);
        if(i == 0){
            return new Result(-1,"删除失败!");
        }
        return new Result();
    }

    @Override
    public Result modifyUser(UserDto userDto) {
        //修改的过程中，部门不能少
        int i = userMapper.selectDeptIsExist(userDto);
        if(i == 0){
            return new Result(-1,"部门不能为空!");
        }
        //修改的过程中，要确保修改的数据，数据库表里面是不存在的
        int j = userMapper.selectUsersIsExist(userDto);
        if(j == 1){
            return new Result(-1,"记录已经存在，请重新修改!");
        }
        int n = userMapper.updateUser(userDto);
        if(n == 0){
            return new Result(-1,"修改失败!");
        }
        return new Result();
    }

}
